package winOrDie.Net.Client;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * This public class provides {@link WoD_Peer} XML editor 
 *
 */
public class WoD_Peer_Editor {

	/**
	 * {@link WoD_Peer_Editor} XML Elements and attributes
	 */
	public static class XML_ELEMENTS_WoD_Peer
	{
		public static String Element_Peer = "WoD_Peer";
		public static String Attribute_UserName = "UserName";
		public static String Attribute_ID = "ID";
		public static String Attribute_ScribeNode = "ScribeNode";
		public static String Attribute_HostDirection = "HostDirection";
		public static String Attribute_Voice_UDP_Port = "Voice_UDP_Port";
		public static String Attribute_TCP_Backup = "TCP_Backup";
		public static String Attribute_TEAM = "TEAM";
		public static String Attribute_ROL = "ROL";
		public static String Attribute_ReadyToGO = "Ready_To_Go";
		
		
	}
	
	/**
	 * This public static method returns {@link Element} from {@link WoD_Peer} and {@link Document} passed as parameters
	 * @param woDPeer
	 * @param document
	 * @return {@link Element}
	 */
	public static Element get_Element_From_WoD_Peer(
			WoD_Peer woDPeer,
			Document document)
	{
		Element element_WoD_Peer = document.createElement(XML_ELEMENTS_WoD_Peer.Element_Peer);
		element_WoD_Peer.setAttribute(XML_ELEMENTS_WoD_Peer.Attribute_UserName, woDPeer.getUserName());
		element_WoD_Peer.setAttribute(XML_ELEMENTS_WoD_Peer.Attribute_ID, String.valueOf(woDPeer.getCliend_Id()));
		element_WoD_Peer.setAttribute(XML_ELEMENTS_WoD_Peer.Attribute_ScribeNode, String.valueOf(woDPeer.getScribeNode_ID()));
		element_WoD_Peer.setAttribute(XML_ELEMENTS_WoD_Peer.Attribute_HostDirection, woDPeer.getHostDirection());
		element_WoD_Peer.setAttribute(XML_ELEMENTS_WoD_Peer.Attribute_Voice_UDP_Port, String.valueOf(woDPeer.getVoice_UDP_Port()));
		element_WoD_Peer.setAttribute(XML_ELEMENTS_WoD_Peer.Attribute_TCP_Backup, String.valueOf(woDPeer.getTcp_backup()));
		element_WoD_Peer.setAttribute(XML_ELEMENTS_WoD_Peer.Attribute_TEAM, String.valueOf(woDPeer.getTeam()));
		element_WoD_Peer.setAttribute(XML_ELEMENTS_WoD_Peer.Attribute_ROL, String.valueOf(woDPeer.getROL()));
		element_WoD_Peer.setAttribute(XML_ELEMENTS_WoD_Peer.Attribute_ReadyToGO, Boolean.toString(woDPeer.isReadyToGo()));
		
		return element_WoD_Peer;
	}
	
	
	/**
	 * This public static method returns {@link WoD_Peer} object from {@link Element} passed as parameter
	 * @param element
	 * @return {@link WoD_Peer}
	 * @throws Exception
	 */
	public static WoD_Peer get_WoD_Peer_From_Element(Element element) throws Exception
	{
		if (!element.getNodeName().equals(XML_ELEMENTS_WoD_Peer.Element_Peer))
		{
			Exception exception = new Exception("The element is not a WoD_Peer element.");
			throw exception;
		}
		
		WoD_Peer woDPeer = new WoD_Peer();
		
		/**
		 * Username
		 */
		woDPeer.setUserName(
				element.getAttribute(XML_ELEMENTS_WoD_Peer.Attribute_UserName)
				);
		
		/**
		 * Client ID
		 */
		woDPeer.setCliend_Id(
				Integer.valueOf(
						element.getAttribute(XML_ELEMENTS_WoD_Peer.Attribute_ID)
						)
				);
		
		/**
		 * Scribe Node ID
		 */
		woDPeer.setScribeNode_ID(				
				element.getAttribute(XML_ELEMENTS_WoD_Peer.Attribute_ScribeNode)
				);
		
		/**
		 * Host direction
		 */
		woDPeer.setHostDirection(
				element.getAttribute(XML_ELEMENTS_WoD_Peer.Attribute_HostDirection)
				);
		
		/**
		 * Voice UDP Port
		 */
		woDPeer.setVoice_UDP_Port(
				Integer.valueOf(
						element.getAttribute(XML_ELEMENTS_WoD_Peer.Attribute_Voice_UDP_Port)
						)
				);
		
		/**
		 * TCP Backup
		 */
		woDPeer.setTcp_backup(
				Integer.valueOf(
						element.getAttribute(XML_ELEMENTS_WoD_Peer.Attribute_TCP_Backup)
						)
				);
		
		/**
		 * Team
		 */
		woDPeer.setTeam(
				Integer.valueOf(
						element.getAttribute(XML_ELEMENTS_WoD_Peer.Attribute_TEAM)
						)
				);
		
		
		/**
		 * ROL
		 */
		woDPeer.setROL(
				Integer.valueOf(
						element.getAttribute(XML_ELEMENTS_WoD_Peer.Attribute_ROL)
						)
				);
		
		/**
		 * Ready to go
		 */
		woDPeer.setReadyToGo(
				Boolean.valueOf(
						element.getAttribute(XML_ELEMENTS_WoD_Peer.Attribute_ReadyToGO)
						)
				
				);
		
		return woDPeer;
		
	}
}
